package com.mb.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mb.app.constants.Constants;
import com.mb.app.service.*;
import com.mb.common.utils.Base64Utils;
import com.mb.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * redis 消息订阅处理器
 * @author
 * @create 2018-05-22 下午5:38
 **/
@Service
@Slf4j
public class RedisReceiver {


    @Autowired
    private IProductPvUvService productPvUvService;

    @Autowired
    private IUserRegService userRegService;

    @Autowired
    private IAppChannelPvUvService appChannelPvUvService;

    @Autowired
    private IReportInfoService reportInfoService;


    /**
     * 处理订阅消息
     * @param message
     */
    public void receiveMessage(String message) {
        log.info("RedisReceiver message :"+message);

        try {
            String redisMsg = Base64Utils.decode(message);
            log.info("redisMsg :"+redisMsg);

            redisMsg = redisMsg.replaceAll("\\\\","");
            JSONObject topicJson = JSONObject.parseObject(redisMsg);
            if(topicJson != null){
                String topic = topicJson.getString("topic");
                if(Constants.RedisTopic.Channel_MSG.getCode().equals(topic)){
                    JSONObject msgJson = topicJson.getJSONObject("msg");
                    String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
                    Long appId = msgJson.getLong("appId");
                    Long channelId = msgJson.getLong("channelId");
                    Long appChannelId = msgJson.getLong("appChannelId");

                    //渠道
                    log.info("startTime;;",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    reportInfoService.insertChanelRepotInfo(appId,channelId,appChannelId,reportDate);
                    log.info("endTime;;",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));  
                    //应用渠道
                    if(appChannelId != null){
                        appChannelPvUvService.runSingle(reportDate,appId,appChannelId);
                    }
                }else if(Constants.RedisTopic.ProductPVUV_MSG.getCode().equals(topic)){
                    JSONObject msgJson = topicJson.getJSONObject("msg");

                    String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
                    Long appId = msgJson.getLong("appId");
                    Long productId = msgJson.getLong("productId");
                    Long channelId = msgJson.getLong("channelId");
                    Long appChannelId = msgJson.getLong("appChannelId");

                    log.info("startTime;;",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    productPvUvService.runSingle(reportDate,appId,productId);
                    log.info("endTime;;",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    //渠道
                    if(channelId != null){
                        reportInfoService.insertChanelRepotInfo(appId,channelId,appChannelId,reportDate);
                    }

                    //应用渠道
                    if(appChannelId != null){
                        appChannelPvUvService.runSingle(reportDate,appId,appChannelId);
                    }
                }else if(Constants.RedisTopic.Reg_MSG.getCode().equals(topic)){
                    JSONObject msgJson = topicJson.getJSONObject("msg");

                    String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
                    Long appId = msgJson.getLong("appId");
                    Long channelId = msgJson.getLong("channelId");
                    Long appChannelId = msgJson.getLong("appChannelId");

                    userRegService.runSingle(reportDate,appId);
                    //渠道
                    if(channelId != null){
                        reportInfoService.insertChanelRepotInfo(appId,channelId,appChannelId,reportDate);
                    }

                    //应用渠道
                    if(appChannelId != null){
                        appChannelPvUvService.runSingle(reportDate,appId,appChannelId);
                    }
                }else if(Constants.RedisTopic.AppChannel_MSG.getCode().equals(topic)){
                    JSONObject msgJson = topicJson.getJSONObject("msg");

                    String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
                    Long appId = msgJson.getLong("appId");
                    Long channelId = msgJson.getLong("channelId");
                    Long appChannelId = msgJson.getLong("appChannelId");
                    //应用渠道
                    appChannelPvUvService.runSingle(reportDate,appId,appChannelId);

                    //渠道
                    if(channelId != null){
                        reportInfoService.insertChanelRepotInfo(appId,channelId,appChannelId,reportDate);
                    }
                }
            }
        } catch (Exception e) {
            log.error("receiveMessage Exception",e);
        }
    }

}
